package com.allenyuan.gui;

import javax.swing.BoxLayout;
import javax.swing.JRadioButton;

import com.allenyuan.proto.PrintTask.PrintTaskType;

import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.ButtonGroup;
import java.awt.Font;
import java.awt.event.ActionEvent;

public class ModeSelectPanel extends AbstractPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1758655648081560754L;
	
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	private JRadioButton gcodeModeButton;
	
	private JRadioButton bitmapModeButton;

	public ModeSelectPanel(GuiContext context) {
		super(context);
		optionPanel.setLayout(new BoxLayout(optionPanel, BoxLayout.Y_AXIS));
		
		gcodeModeButton = new JRadioButton("GCode方式");
		gcodeModeButton.setActionCommand("gcodeMode");
		gcodeModeButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		buttonGroup.add(gcodeModeButton);
		gcodeModeButton.setAlignmentY(Component.TOP_ALIGNMENT);
		optionPanel.add(gcodeModeButton);
		
		bitmapModeButton = new JRadioButton("位图方式");
		bitmapModeButton.setActionCommand("bitmapMode");
		bitmapModeButton.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		buttonGroup.add(bitmapModeButton);
		optionPanel.add(bitmapModeButton);
	}

	@Override
	protected String getGuideText() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		sb.append("本雕刻机支持两种输出方式：\n");
		sb.append("1、GCode方式\n");
		sb.append("GCode是在数控设备中使用十分频繁的代码，本雕刻机兼容了常用GCode指令，可以直接解释运行GCode代码，雕刻出用户指定的轮廓.\n");
		sb.append("2、位图方式\n");
		sb.append("位图方式类似于普通打印机的行扫描的方式，逐行进行雕刻，适用于比较复杂的图形.");
		return sb.toString();
	}

	@Override
	protected String getGuideTitle() {
		// TODO Auto-generated method stub
		return "模式选择";
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals(nextButton.getActionCommand())) {
			if (buttonGroup.getSelection().getActionCommand().equals(gcodeModeButton.getActionCommand())) {
				((CardLayout)getParent().getLayout()).show(getParent(), GCodeImportPanel.class.getName());
				getContext().setPrintType(PrintTaskType.PRINT_TASK_GCODE);
			}
			else if (buttonGroup.getSelection().getActionCommand().equals(bitmapModeButton.getActionCommand())) {
				((CardLayout)getParent().getLayout()).show(getParent(), BitmapImportPanel.class.getName());
				getContext().setPrintType(PrintTaskType.PRINT_TASK_BITMAP);
			}
		}
		else if (e.getActionCommand().equals(lastButton.getActionCommand())) {
			((CardLayout)getParent().getLayout()).show(getParent(), ConnectionTestPanel.class.getName());
		}
	}

}
